# -*-coding:utf-8 -*-
#!/usr/bin/env python

#function: 水位线数值自动识别

import cv2
import numpy as np
import os
from preprocessing import pre_process  #[自定义]预处理函数模块
from char_locate import char_loc  #[自定义]字符提取函数模块
from char_recognize_hamming import char_rec #[自定义]字符识别函数库模块


getpath = 'get' #所获取字符的路径
libpath = 'lib' #匹配库路径
srcpath = 'src' #原图像路径
touchline = 0 #十个位、小数位压线检测标志位
result=[]     #结果数值字符串
for k,path0 in enumerate(os.listdir(srcpath)):

    print('\n\n\n-*-*-*-*-*-*-*-*-*-*pic:%d*-*-*-*-*-*-*-*-*-*-*\n'%(k+1))
    ##一、提取水位信息字符并保存

    #读入图像 
    src = cv2.imread(srcpath+'\\'+path0)
    #cv2.imshow('pic%d'%(k+1),src)
    #预处理
    img = pre_process(src)
    #cv2.imshow('src',src)
    #cv2.imshow('pre',img)
    #字符提取
    touchline = char_loc(img, detail=0)


    ##二、字符识别

    num=[] #字符识别结果

    for path in os.listdir(getpath):
        get_char = cv2.imread(getpath+'\\'+path)
        tmp = char_rec(get_char,libpath, detail=1)
        num.append(tmp)


    #打印字符识别结果
    print('\n---------recognition results:-----------')
    num_max = max(num)
    for i in range(len(num)):
        if num[i]!=10:
            print('character',i+1, ':%d\n'%num[i])
        else:
            print('character',i+1, ':%s\n'%'M')


    print('---------final result:-----------\n')


    #最终数值及校正
    if touchline==1:
        res = 10*num[0]+num[1]-1+0.1*(num[2]-2)
    else:
        res = 10*num[0]+num[1]-1+0.1*num[2]
    result.append(path0+':%s\n'%res)
    print('final result: %.1f'%res)

    pause_now = input('\npause, press any key to continue!') #处理完一副图片暂停

    print('final result: %.1f'%res)   

with open('result\\result.txt','w') as file:
    for res in result:
        file.write(res)
end = input('\n\n -*-*press any key to quit*-*-')


